package pl.com.kyhu.dietex3000.GUI.components;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.wb.swt.SWTResourceManager;

import pl.com.kyhu.dietex3000.database.beans.Ingredient;
import pl.com.kyhu.dietex3000.database.beans.IngredientCategory;
import pl.com.kyhu.dietex3000.database.management.DataManagement;
import pl.com.kyhu.dietex3000.utils.Constants;

public class IngredientTree extends Composite {

	Tree tree;

	private Map<TreeItem, Ingredient> treeIngredients;

	public IngredientTree(Composite parent, int style) {
		super(parent, SWT.BORDER);
		tree = new Tree(this, SWT.BORDER | SWT.FULL_SELECTION);
		setLayout(new FillLayout(SWT.HORIZONTAL));
		refreshData();

		Menu popupMenu = new Menu(this);
		tree.setMenu(popupMenu);

		MenuItem refreshMenuItem = new MenuItem(popupMenu, SWT.NONE);
		refreshMenuItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				refreshData();
			}
		});
		refreshMenuItem.setText("Od\u015Bwie\u017C");

		tree.pack();

	}

	public void refreshData() {
		tree.removeAll();
		treeIngredients = new HashMap<TreeItem, Ingredient>();
		tree.setLayout(new FillLayout(SWT.HORIZONTAL));
		List<IngredientCategory> categories = DataManagement.getIngredientSubCategories(Constants.ingCategoryNode);

		boolean emptyItem;
		boolean emptySubItem;
		for (IngredientCategory category : categories) {
			emptyItem = true;
			TreeItem item = new TreeItem(tree, SWT.CHECK);
			item.setText(category.getName());
			item.setFont(SWTResourceManager.getFont("Microsoft Sans Serif", 8, SWT.BOLD));
			item.setBackground(SWTResourceManager.getColor(232, 232, 232));

			for (IngredientCategory subCategory : DataManagement.getIngredientSubCategories(category)) {
				emptySubItem = true;

				TreeItem subItem = new TreeItem(item, SWT.CHECK);
				subItem.setText(subCategory.getName());
				subItem.setFont(SWTResourceManager.getFont("Microsoft Sans Serif", 8, SWT.BOLD));
				subItem.setBackground(SWTResourceManager.getColor(239, 239, 239));

				for (Ingredient ingredient : DataManagement.getIngredientsWithCategory(subCategory)) {
					emptySubItem = false;
					TreeItem ingredientItem = new TreeItem(subItem, SWT.CHECK);
					ingredientItem.setText(ingredient.getName());
					treeIngredients.put(ingredientItem, ingredient);
				}

				if (emptySubItem) subItem.dispose();
				else
					emptyItem = false;

			}
			for (Ingredient ingredient : DataManagement.getIngredientsWithCategory(category)) {
				emptyItem = false;
				TreeItem ingredientItem = new TreeItem(item, SWT.CHECK);
				ingredientItem.setText(ingredient.getName());
				treeIngredients.put(ingredientItem, ingredient);
			}

			if (emptyItem) item.dispose();
		}

	}

	public TreeItem getSelection() {
		return tree.getSelection()[0];

	}

	public Map<TreeItem, Ingredient> getIngredients() {
		return treeIngredients;
	}

}
